<template>
    <view class="outvi">
        <view class="homed">
            <view class="ha">首单额度</view>
            <view class="hb"><input type="number" v-model="setObj.initOrderPosition" @blur="handleBlur" :placeholder="`最小值: ${minInitOrderPosition}`"/></view>
			<view class="hc" v-if="setObj.exchangeType==0">USDT</view>
			<view class="hc" v-if="setObj.exchangeType==1">USDT</view>
			<view class="hc" v-if="setObj.exchangeType==2">USDT</view>
        </view>
        <view class="tioed">
             补仓设置
        </view>
        <!-- <view class="homed">
            <view class="ha">补仓上限</view>
            <view class="hb"><input type="number" v-model.number="setObj.makeOrderNum" @input="btKeyUp"/></view>
            <view class="hc">次</view>
        </view>
		<view class="homed">
		    <view class="ha">补仓止盈率</view>
		    <view class="hb"><input type="number" v-model="setObj.stopProfitRatio" /></view>
		    <view class="hc">%</view>
		</view>
		<view class="homed" @click="setimgfn">
		    <view class="ha">补仓跌幅率</view>
		    <view class="hb" style="color: #ADB0BB;">已设置</view>
		    <image src="../../static/img/dbx.png"></image>
		</view>
		<view class="homed">
		    <view class="ha">补仓止盈回调率</view>
		    <view class="hb"><input type="number" v-model="setObj.stopProfitBackRatio" /></view>
		    <view class="hc">%</view>
		</view>
		<view class="homed">
		    <view class="ha">补仓回调率</view>
		    <view class="hb"><input type="number" v-model="setObj.addPositionBackRatio" /></view>
		    <view class="hc">%</view>
		</view>
		<view class="tioed">
		     加仓设置
		</view>
		<view class="homed">
		    <view class="ha">加仓上限</view>
		    <view class="hb"><input type="number" v-model.number="setObj.antiMakeOrderNum" @input="btKeyUp2"/></view>
		    <view class="hc">次</view>
		</view>
		<view class="homed">
		    <view class="ha">加仓止盈率</view>
		    <view class="hb"><input type="number" v-model="setObj.antiStopProfitRatio" /></view>
		    <view class="hc">%</view>
		</view>
		<view class="homed" @click="setimgfn2">
		    <view class="ha">加仓涨幅率</view>
		    <view class="hb" style="color: #ADB0BB;">已设置</view>
		    <image src="../../static/img/dbx.png"></image>
		</view> -->
		<!-- <view class="homed">
		    <view class="ha">多单对冲跌幅</view>
		    <view class="hb"><input type="number" v-model="setObj.buyStopRate" /></view>
		    <view class="hc">%</view>
		</view>
		<view class="homed">
		    <view class="ha">空单对冲涨幅</view>
		    <view class="hb"><input type="number" v-model="setObj.sellStopRate" /></view>
		    <view class="hc">%</view>
		</view>
		<view class="homed">
		    <view class="ha">解套风控需达到的补仓次数</view>
		    <view class="hb"><input type="number" v-model="setObj.getOffAddPositionNum" /></view>
		    <view class="hc">次</view>
		</view>
		<view class="homed">
		    <view class="ha">运行模式(0.正常 1.快进快出)</view>
		    <view class="hb"><input type="number" v-model="setObj.getOffNeedTime" /></view>
		</view>
		<view class="homed">
		    <view class="ha">解套风控的指定解套浮动盈亏线</view>
		    <view class="hb"><input type="number" v-model="setObj.getOffProfitLine" /></view>
		    <view class="hc">USDT</view>
		</view>
		<view class="homed">
		    <view class="ha">对冲线前止损率</view>
		    <view class="hb"><input type="number" v-model="setObj.stopLossesRatio" /></view>
		    <view class="hc">%</view>
		</view>
		<view class="homed">
		    <view class="ha">对冲线后止损率</view>
		    <view class="hb"><input type="number" v-model="setObj.stopLossProfit" /></view>
		    <view class="hc">%</view>
		</view>
		<view class="homed">
		    <view class="ha">对冲线补仓次数</view>
		    <view class="hb"><input type="number" v-model="setObj.totalStopLossNumLimitAddNum" /></view>
		    <view class="hc">次</view>
		</view>
		<view class="homed">
		    <view class="ha">倍数模式(0.固定 1.动态)</view>
		    <view class="hb"><input type="text" v-model="setObj.addPositionTimesModelSwitch" /></view>
		</view> -->
		<view class="tioed">
		     其他设置
		</view>
		<view class="homed">
		    <view class="ha">循环策略(0.循环 1.单次)</view>
		    <view class="hb"><input type="number" v-model="setObj.strategyType" /></view>
		</view>
		<view class="homed">
		    <view class="ha">杠杆倍数</view>
		    <view class="hb"><input type="number" v-model="setObj.leverRate" /></view>
		</view>
        <view class="maskd" v-if="issets" @click="qxBtnfn">
            <view class="modelsheet" @click.stop.prevent="stopPenetrate">
                <view class="stip">
                    <view class="bcdf">
                        补仓跌幅
                    </view>
                    <view class="cancelb" @click="canelbtn">
                        取消
                    </view>
                </view>
                <view style="height: 640rpx;overflow: auto;" @touchmove.stop @touch.stop>
                    <view class="tced">
                        <view class="ha">首次补仓</view>
                        <view class="hb"><input type="number" v-model="setObj.addPosition1" /></view>
                        <view class="hc">%</view>
                    </view>
                    <view class="tced">
                        <view class="ha">第2次补仓</view>
                        <view class="hb"><input type="number" v-model="setObj.addPosition2" /></view>
                        <view class="hc">%</view>
                    </view>
                    <view class="tced">
                        <view class="ha">第3次补仓</view>
                        <view class="hb"><input type="number" v-model="setObj.addPosition3" /></view>
                        <view class="hc">%</view>
                    </view>
                    <view class="tced">
                        <view class="ha">第4次补仓</view>
                        <view class="hb"><input type="number" v-model="setObj.addPosition4" /></view>
                        <view class="hc">%</view>
                    </view>
                    <view class="tced">
                        <view class="ha">第5次补仓</view>
                        <view class="hb"><input type="number" v-model="setObj.addPosition5" /></view>
                        <view class="hc">%</view>
                    </view>
                    <view class="tced">
                        <view class="ha">第6次补仓</view>
                        <view class="hb"><input type="number" v-model="setObj.addPosition6" /></view>
                        <view class="hc">%</view>
                    </view>
                    <view class="tced">
                        <view class="ha">第7次补仓</view>
                        <view class="hb"><input type="number" v-model="setObj.addPosition7" /></view>
                        <view class="hc">%</view>
                    </view>
					<view class="tced">
					    <view class="ha">第8次补仓</view>
					    <view class="hb"><input type="number" v-model="setObj.addPosition8" /></view>
					    <view class="hc">%</view>
					</view>
					<view class="tced">
					    <view class="ha">第9次补仓</view>
					    <view class="hb"><input type="number" v-model="setObj.addPosition9" /></view>
					    <view class="hc">%</view>
					</view>
					<view class="tced">
					    <view class="ha">第10次补仓</view>
					    <view class="hb"><input type="number" v-model="setObj.addPosition10" /></view>
					    <view class="hc">%</view>
					</view>
					<view class="tced">
					    <view class="ha">第11次补仓</view>
					    <view class="hb"><input type="number" v-model="setObj.addPosition11" /></view>
					    <view class="hc">%</view>
					</view>
					<view class="tced">
					    <view class="ha">第12次补仓</view>
					    <view class="hb"><input type="number" v-model="setObj.addPosition12" /></view>
					    <view class="hc">%</view>
					</view>
					<view class="tced">
					    <view class="ha">第13次补仓</view>
					    <view class="hb"><input type="number" v-model="setObj.addPosition13" /></view>
					    <view class="hc">%</view>
					</view>
					<view class="tced">
					    <view class="ha">第14次补仓</view>
					    <view class="hb"><input type="number" v-model="setObj.addPosition14" /></view>
					    <view class="hc">%</view>
					</view>
					<view class="tced">
					    <view class="ha">第15次补仓</view>
					    <view class="hb"><input type="number" v-model="setObj.addPosition15" /></view>
					    <view class="hc">%</view>
					</view>
					<view class="tced">
					    <view class="ha">第16次补仓</view>
					    <view class="hb"><input type="number" v-model="setObj.addPosition16" /></view>
					    <view class="hc">%</view>
					</view>
					<view class="tced">
					    <view class="ha">第17次补仓</view>
					    <view class="hb"><input type="number" v-model="setObj.addPosition17" /></view>
					    <view class="hc">%</view>
					</view>
					<view class="tced">
					    <view class="ha">第18次补仓</view>
					    <view class="hb"><input type="number" v-model="setObj.addPosition18" /></view>
					    <view class="hc">%</view>
					</view>
					<view class="tced">
					    <view class="ha">第19次补仓</view>
					    <view class="hb"><input type="number" v-model="setObj.addPosition19" /></view>
					    <view class="hc">%</view>
					</view>
					<view class="tced">
					    <view class="ha">第20次补仓</view>
					    <view class="hb"><input type="number" v-model="setObj.addPosition20" /></view>
					    <view class="hc">%</view>
					</view>
                    <button type="default" @click="quedbtnfn">确定</button>
                </view>
            </view>
        </view>
		
		<view class="maskd" v-if="issets2" @click="qxBtnfn2">
		    <view class="modelsheet" @click.stop.prevent="stopPenetrate2">
		        <view class="stip">
		            <view class="bcdf">
		                加仓涨幅
		            </view>
		            <view class="cancelb" @click="canelbtn2">
		                取消
		            </view>
		        </view>
		        <view style="height: 640rpx;overflow: auto;" @touchmove.stop @touch.stop>
		            <view class="tced">
		                <view class="ha">首次加仓</view>
		                <view class="hb"><input type="number" v-model="setObj.antiAddPosition1" /></view>
		                <view class="hc">%</view>
		            </view>
		            <view class="tced">
		                <view class="ha">第2次加仓</view>
		                <view class="hb"><input type="number" v-model="setObj.antiAddPosition2" /></view>
		                <view class="hc">%</view>
		            </view>
		            <view class="tced">
		                <view class="ha">第3次加仓</view>
		                <view class="hb"><input type="number" v-model="setObj.antiAddPosition3" /></view>
		                <view class="hc">%</view>
		            </view>
		            <view class="tced">
		                <view class="ha">第4次加仓</view>
		                <view class="hb"><input type="number" v-model="setObj.antiAddPosition4" /></view>
		                <view class="hc">%</view>
		            </view>
		            <view class="tced">
		                <view class="ha">第5次加仓</view>
		                <view class="hb"><input type="number" v-model="setObj.antiAddPosition5" /></view>
		                <view class="hc">%</view>
		            </view>
		            <view class="tced">
		                <view class="ha">第6次加仓</view>
		                <view class="hb"><input type="number" v-model="setObj.antiAddPosition6" /></view>
		                <view class="hc">%</view>
		            </view>
		            <view class="tced">
		                <view class="ha">第7次加仓</view>
		                <view class="hb"><input type="number" v-model="setObj.antiAddPosition7" /></view>
		                <view class="hc">%</view>
		            </view>
					<view class="tced">
					    <view class="ha">第8次加仓</view>
					    <view class="hb"><input type="number" v-model="setObj.antiAddPosition8" /></view>
					    <view class="hc">%</view>
					</view>
					<view class="tced">
					    <view class="ha">第9次加仓</view>
					    <view class="hb"><input type="number" v-model="setObj.antiAddPosition9" /></view>
					    <view class="hc">%</view>
					</view>
					<view class="tced">
					    <view class="ha">第10次加仓</view>
					    <view class="hb"><input type="number" v-model="setObj.antiAddPosition10" /></view>
					    <view class="hc">%</view>
					</view>
		            <button type="default" @click="quedbtnfn2">确定</button>
		        </view>
		    </view>
		</view>
    </view>
</template>

<script>
    let uinfo;
    import request from "../../untils/request.js";
    import {
        multiply,
        divide
    } from "../../untils/math.js";
    const math = require('mathjs')
    export default {
        data() {
            return {
				minInitOrderPosition: 0,
                issets: false,
				issets2: false,
                setObj: {},
                op: {},
                qxobj: {}
            }
        },
        onLoad(options) {
            uinfo = uni.getStorageSync('userInfo');
            this.op = options;
			this.getMinInitOrderPosition(options.dest, options.sourc, options.exchangety,options.direction);
            this.defaustratgyFn(options.dest, options.sourc, options.exchangety,options.direction,options.leverRate)
        },
        onShow() {
        },
        onNavigationBarButtonTap: function(e) {
            this.updetafn();
        },
        onPullDownRefresh() {
            this.defaustratgyFn(this.op.dest, this.op.sourc, this.op.exchangety,this.op.direction,this.op.leverRate)
        },

        methods: {
			handleBlur() {
			      if (this.setObj.initOrderPosition < this.minInitOrderPosition) {
			        uni.showToast({
			          title: '首单额度不能小于最小允许值，请重新设置',
			          icon: 'none',
			        });
			        this.setObj.initOrderPosition = '';
					return false
			      }else{
					return true  
				  }
			},
			// 获取最小首单额度
			getMinInitOrderPosition(dest, sourc, exchangety,direction) {
				request.postAssets('/assets/quantOperate/getSmallestInitOrderPosition', {
					userId: uinfo.userId,
					exchangeType:exchangety,
					destinationCoin:dest,
					sourceCoin:sourc,
					direction:direction
				}).then(res => {
				    if (res.code == '200') {
						this.minInitOrderPosition = res.data.smallestInitOrderPosition;
						uni.stopPullDownRefresh();
				    }else{
						uni.stopPullDownRefresh();
					}
				})
			},
            btKeyUp(e) {
                if(e.target.value.indexOf('.') > -1){
                   this.setObj.makeOrderNum= '';
                }
            },
			btKeyUp2(e) {
			    if(e.target.value.indexOf('.') > -1){
			       this.setObj.antiMakeOrderNum= '';
			    }
			},
            changeNumber(e) {
                var isnum = /^\d+$/.test(e.target.value);
                let reg=/^\d+$/;
                if(isnum==false){
                    console.log(e.target.value)
                   e.target.value= '';
                }
            },

            //默认数据 
            defaustratgyFn(dest, sourc, exchangety,direction,leverRate) {
                request.postAssets('/assets/quantOperate/queryStrategy', {
					userId: uinfo.userId, //用户ID
					destinationCoin: dest, //交易对目的币种
					sourceCoin: sourc, //交易对源币种
                    exchangeType: exchangety, //交易所类型(0.okex 1.huobi 2.binance)
					direction: direction //多空方向(buy:多单 sell:空单)
                }).then(res => {
                    console.log(res)
                    if (res.code == '200') {
                        uni.stopPullDownRefresh();
                        this.setObj = res.data;
                        this.qxobj = res.data;
                    } else {
                        uni.stopPullDownRefresh();
                        uni.showToast({
                            icon: "none",
                            title: res.message,
                        });
                    }
                })
            },
            updetafn() {
				if(!this.handleBlur()){
					return
				}
                let _this = this;
                function substr(str) {
                    var s = str + "";
                    if(s.indexOf(".")!='-1'){
                        var str = s.substring(0,s.indexOf(".") + 4);
                    }
                    return str
                }
                this.setObj.stopProfitRatio = substr(_this.setObj.stopProfitRatio);
				this.setObj.antiStopProfitRatio = substr(_this.setObj.antiStopProfitRatio);
                this.setObj.stopProfitBackRatio = substr(_this.setObj.stopProfitBackRatio);
				this.setObj.stopLossesRatio = substr(_this.setObj.stopLossesRatio);
				this.setObj.stopLossProfit = substr(_this.setObj.stopLossProfit);
                this.setObj.addPositionBackRatio = substr(_this.setObj.addPositionBackRatio);
                this.setObj.addPosition1 = substr(_this.setObj.addPosition1);
                this.setObj.addPosition2 = substr(_this.setObj.addPosition2);
                this.setObj.addPosition3 = substr(_this.setObj.addPosition3);
                this.setObj.addPosition4 = substr(_this.setObj.addPosition4);
                this.setObj.addPosition5 = substr(_this.setObj.addPosition5);
                this.setObj.addPosition6 = substr(_this.setObj.addPosition6);
                this.setObj.addPosition7 = substr(_this.setObj.addPosition7);
				
				this.setObj.antiAddPosition1 = substr(_this.setObj.antiAddPosition1);
				this.setObj.antiAddPosition2 = substr(_this.setObj.antiAddPosition2);
				this.setObj.antiAddPosition3 = substr(_this.setObj.antiAddPosition3);
				this.setObj.antiAddPosition4 = substr(_this.setObj.antiAddPosition4);
				this.setObj.antiAddPosition5 = substr(_this.setObj.antiAddPosition5);
				this.setObj.antiAddPosition6 = substr(_this.setObj.antiAddPosition6);
				this.setObj.antiAddPosition7 = substr(_this.setObj.antiAddPosition7);
                this.setObj.antiMakeOrderNum = Number(this.setObj.antiMakeOrderNum);
				this.setObj.direction = substr(_this.setObj.direction);
				this.setObj.leverRate = Number(this.setObj.leverRate);
				
                uni.showLoading({
                    mask: true
                });
                request.postAssets('/assets/quantOperate/updateStrategy', this.setObj).then(res => {
                    if (res.code == '200') {
                        uni.hideLoading();
                        uni.showToast({
                            icon: "none",
                            title: '保存成功',
                        });
                    } else {
                        uni.hideLoading();
                        uni.showToast({
                            icon: "none",
                            title: res.message,
                        });
                    }
                })
            },
            //弹框取消按钮
            canelbtn() {
                /* this.defaustratgyFn(this.op.dest, this.op.sourc, this.op.exchangety,this.op.direction,this.op.leverRate)
                this.setObj = this.qxobj; */
                this.issets = false;
            },
			canelbtn2() {
			    /* this.defaustratgyFn(this.op.dest, this.op.sourc, this.op.exchangety,this.op.direction,this.op.leverRate)
			    this.setObj = this.qxobj; */
			    this.issets2 = false;
			},
            stopPenetrate() {
                return;
            },
			stopPenetrate2() {
			    return;
			},
            //设置
            setimgfn() {
                this.issets = true;
            },
			//设置
			setimgfn2() {
			    this.issets2 = true;
			},
            //取消设置
            qxBtnfn() {
                this.issets = false;
            },
			qxBtnfn2() {
			    this.issets2 = false;
			},
            //确定
            quedbtnfn() {
                this.issets = false;
            },
			quedbtnfn2() {
			    this.issets2 = false;
			}
        }
    }
</script>

<style>
    .outvi {
        background: #F5F5F5;
        padding: 1px 0 100% 0;
    }

    .homed {
        width: 355px;
        height: 46px;
        background: #FFFFFF;
        border-radius: 6px;
        margin: 20px auto 0px;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .homed .ha {
        font-size: 14px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        line-height: 24px;
        color: #6E6E6E;
        margin: 0 0 0 20px;
    }

    .homed .hb {
        font-size: 18px;
        font-family: Aileron;
        font-weight: 500;
        line-height: 26px;
        color: #333333;
        margin: 0 20rpx 0 40rpx;
        width: 210rpx;
    }

    .homed .hb input {
        text-align: center;
        border-bottom: 1px solid rgba(0, 0, 0, .1);
    }

    .homed .hc {
        font-size: 18px;
        font-family: Aileron;
        font-weight: 400;
        line-height: 26px;
        color: #333333;
        margin: 0 20px 0 0;
        width: 100rpx;
        text-align: right;
    }

    .homed image {
        width: 15px;
        height: 8px;
        margin: 0 20px 0 0;
    }

    .tioed {
        font-size: 18px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        line-height: 24px;
        color: #838AA4;
        margin: 20px 0 0 20px;
    }

    .maskd {
        position: fixed;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.4);
    }

    .modelsheet {
        width: 100%;
        height: 840rpx;
        background: #FFFFFF;
        position: fixed;
        left: 0;
        bottom: 0;
        overflow-y: auto;
    }

    .modelsheet button {
        width: 600rpx;
        height: 38px;
        background: #7F6AC3;
        border-radius: 19px;
        font-size: 14px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        color: #FFFFFF;
        position: fixed;
        left: 0;
        right: 0;
        bottom: 20rpx;
    }

    .modelsheet .tced {
        height: 42px;
        background: #FFFFFF;
        margin: 12rpx 10px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-bottom: 1px solid #EBEBEB;
    }

    .modelsheet .tced .ha {
        font-size: 14px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        line-height: 24px;
        color: #6E6E6E;
        width: 200rpx;
    }

    .modelsheet .tced .hb {
        font-size: 18px;
        font-family: Aileron;
        font-weight: 500;
        line-height: 26px;
        color: #333333;
        margin: 0 0 0 70px;
    }

    .modelsheet .tced .hb input {
        text-align: center;
        border-bottom: 1px solid #7F6AC3;
    }

    .modelsheet .tced .hc {
        font-size: 18px;
        font-family: Aileron;
        font-weight: 400;
        line-height: 26px;
        color: #333333;
    }


    .modelsheet .stip {
        display: flex;
        justify-content: space-between;
        padding: 1px 0 10rpx 0;
        margin: 0 10px;
        border-bottom: 1px solid #EBEBEB;
    }

    .modelsheet .stip .bcdf {
        font-size: 16px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        line-height: 27px;
        color: #333333;
        padding: 10rpx 0 0 0;
    }

    .modelsheet .stip .cancelb {
        font-size: 16px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        line-height: 27px;
        color: #ADB0BB;
        padding: 10rpx 30rpx 10rpx;
    }

    .modelsheet .imgshows {
        display: flex;
        justify-content: space-around;
        margin: 20px 0 0 0;
    }

    .modelsheet .imgshows image {
        width: 60px;
        height: 60px;
    }

    .modelsheet .imgshows text {
        display: block;
        font-size: 14px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        line-height: 24px;
        color: #333333;
    }
</style>
